package com.datang.pet.service.impl;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-06-09 15:06
 * @notify
 * @version 1.0
 */

import com.alibaba.fastjson.JSON;
import com.datang.pet.common.redis.RedisTool;
import com.datang.pet.common.socket.WebSocketServer;
import com.datang.pet.mapper.UserMessageMapper;
import com.datang.pet.mapperExtend.UserAttentionRecordMapperExtend;
import com.datang.pet.pojo.UserMessage;
import com.datang.pet.pojo.UserMessageExample;
import com.datang.pet.service.MessageService;
import com.datang.pet.vo.GetFansListResponse;
import com.datang.pet.vo.GetLikeListResponse;
import com.datang.pet.vo.GetCommentMessageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MessageServiceImpl implements MessageService {

    //异常写入日志
    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MessageServiceImpl.class);

    @Autowired
    private UserMessageMapper userMessageMapper;

    @Autowired
    private RedisTool redisTool;

    @Autowired
    private WebSocketServer webSocketServer;

    @Autowired
    private UserAttentionRecordMapperExtend userAttentionRecordMapperExtend;

    public void getMessage(String userId, String type) {
        //从redis中查询是否有用户id
        String redisUserId = (String) redisTool.get(userId);

        if (redisUserId != null) {
            Map<String, Object> map = new HashMap<>();
            // 1 发布喵文成功
            if (type.equals("1")) {
                map.put("1", "您编辑的喵文已经发布成功.");
            } else if (type.equals("2") || type.equals("5") || type.equals("8")) {
                // 2 获赞
                List<UserMessage> likeMessages = null;
                try {
                    UserMessageExample userMessageExample = new UserMessageExample();
                    UserMessageExample.Criteria criteria = userMessageExample.createCriteria();
                    //指定用户id
                    criteria.andUserIdEqualTo(userId);
                    //未读消息
                    criteria.andIsReadEqualTo("0");
                    //获赞消息
                    ArrayList<String> typeList = new ArrayList<>();
                    //2喵文被点赞
                    typeList.add("2");
                    //5一层评论被点赞
                    typeList.add("5");
                    // 8二层评论被点赞
                    typeList.add("8");
                    criteria.andMessageTypeIn(typeList);
                    likeMessages = userMessageMapper.selectByExample(userMessageExample);
                } catch (Exception e) {
                    log.error("查询用户获赞记录失败");
                }
                map.put("2", likeMessages.size());
            } else if (type.equals("3")) {
                //有新粉丝通知
                List<UserMessage> attentionMessage = null;
                try {
                    UserMessageExample userMessageExample = new UserMessageExample();
                    UserMessageExample.Criteria criteria = userMessageExample.createCriteria();
                    criteria.andUserIdEqualTo(userId);
                    //未读消息
                    criteria.andIsReadEqualTo("0");
                    //新粉丝
                    criteria.andMessageTypeEqualTo("3");
                    attentionMessage = userMessageMapper.selectByExample(userMessageExample);
                } catch (Exception e) {
                    log.error("查询用户新粉丝失败");
                }
                map.put("3", attentionMessage.size());
            } else if (type.equals("4") || type.equals("6") || type.equals("7")) {
                //喵文有回复
                List<UserMessage> attentionMessage = null;
                try {
                    UserMessageExample userMessageExample = new UserMessageExample();
                    UserMessageExample.Criteria criteria = userMessageExample.createCriteria();
                    criteria.andUserIdEqualTo(userId);
                    //未读消息
                    criteria.andIsReadEqualTo("0");
                    ArrayList<String> typeList = new ArrayList<>();
                    //一级评论
                    typeList.add("4");
                    //二级评论
                    typeList.add("6");
                    //有人回复我的一级评论
                    typeList.add("7");
                    criteria.andMessageTypeIn(typeList);
                    attentionMessage = userMessageMapper.selectByExample(userMessageExample);
                } catch (Exception e) {
                    log.error("查询评论消息失败");
                }
                map.put("4", attentionMessage.size());
            } else if (type.equals("9")) {
                map.put("9", 1);
            }
            String jsonDate = JSON.toJSONString(map);
            try {
                webSocketServer.sendMessage(jsonDate, userId);
            } catch (Exception e) {
                log.error("给用户发送消息失败");
            }

        }
    }


    /*
     * @author 顶风少年
     * @date 2019/6/18 18:08
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param userId
     * @result java.util.List<GetFansListResponse>
     * @notify 获取用户粉丝列表
     * version 1.0
     */
    public List<GetFansListResponse> getFansList(String userId, Date time) throws Exception {

        //获取未读关注
        List<GetFansListResponse> fansList = null;
        try {
            fansList = userAttentionRecordMapperExtend.getFansList(userId, time);
        } catch (Exception e) {
            throw new Exception("获取粉丝消息失败");
        }
        //把未读置为已读
        UserMessage userMessage = new UserMessage();
        userMessage.setIsRead("1");
        UserMessageExample userMessageExample = new UserMessageExample();
        UserMessageExample.Criteria criteria = userMessageExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andMessageTypeEqualTo("3");
        try {
            userMessageMapper.updateByExampleSelective(userMessage, userMessageExample);
        } catch (Exception e) {
            throw new Exception("粉丝消息设为已读失败");
        }


        return fansList;
    }

    public List<GetLikeListResponse> getLikeList(String userId, Date time) throws Exception {

        List<GetLikeListResponse> likeList = null;
        try {
            likeList = userAttentionRecordMapperExtend.getLikeList(userId, time);
        } catch (Exception e) {
            throw new Exception("获取点赞消息失败");
        }

        //把未读置为已读
        UserMessage userMessage = new UserMessage();
        userMessage.setIsRead("1");
        UserMessageExample userMessageExample = new UserMessageExample();
        UserMessageExample.Criteria criteria = userMessageExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<String> typeList = new ArrayList<>();
        typeList.add("2");
        typeList.add("5");
        typeList.add("8");
        criteria.andMessageTypeIn(typeList);
        try {
            userMessageMapper.updateByExampleSelective(userMessage, userMessageExample);
        } catch (Exception e) {
            throw new Exception("点赞消息设为已读失败");
        }


        return likeList;
    }

    public List<GetCommentMessageResponse> getCommentMessageList(String userId, Date time) throws Exception {


        List<GetCommentMessageResponse> commentList = null;
        try {
            commentList = userAttentionRecordMapperExtend.getCommentMessageList(userId, time);
        } catch (Exception e) {
            throw new Exception("获取点赞消息失败");
        }


        //把未读置为已读
        UserMessage userMessage = new UserMessage();
        userMessage.setIsRead("1");
        UserMessageExample userMessageExample = new UserMessageExample();
        UserMessageExample.Criteria criteria = userMessageExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<String> typeList = new ArrayList<>();
        typeList.add("4");
        typeList.add("6");
        typeList.add("7");
        criteria.andMessageTypeIn(typeList);
        try {
            userMessageMapper.updateByExampleSelective(userMessage, userMessageExample);
        } catch (Exception e) {
            throw new Exception("评论消息设为已读失败");
        }


        return commentList;
    }
}
